/**
 * created by 小卷毛, 2020/9/17
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.sgcc.pda.lib_tools_database.factory;

import android.text.TextUtils;

import com.sgcc.pda.lib_tools_database.bean.ControlProcess;
import com.sgcc.pda.lib_tools_database.bean.TypicalExp;
import com.sgcc.pda.lib_tools_database.bean.VideoStandard;
import com.sgcc.pda.lib_tools_database.bean.other.ControlProcessDetail;
import com.sgcc.pda.lib_tools_database.bean.other.JSBaseListResult;
import com.sgcc.pda.lib_tools_database.bean.other.JSBaseResult;
import com.sgcc.pda.lib_tools_database.bean.other.ProcessRiskData;
import com.sgcc.pda.lib_tools_database.bean.other.RiskCheckState;
import com.sgcc.pda.lib_tools_database.bean.other.TicketMeasureResult;
import com.sgcc.pda.lib_tools_database.bean.other.TicketMeasureSave;
import com.sgcc.pda.lib_tools_database.bean.other.TicketMeasureSaveJS;
import com.sgcc.pda.lib_tools_database.bean.ticket.TicketDetail;
import com.sgcc.pda.lib_tools_database.bean.ticket.TicketForType;
import com.sgcc.pda.tools.JsonUtils;

import java.util.List;

/**
 * 描 述：JSBridge接口结果封装
 * 作 者：小卷毛  2020-09-17 16:40
 * 修改描述：增加参数字段
 * 修 改 人：
 * 修改版本：v1.2.1
 */
public class JSResultUtil {

    /**
     * 获取知识库 专业列表 数据
     */
    public static String getProfession() {
        List<ControlProcess> list = new KnowledgeCheck().getProfession();

        JSBaseListResult<ControlProcess> result = new JSBaseListResult<>();
        if (list == null || list.isEmpty()) {
            result.setRT_F("40");
            result.setRT_D("未查询到此数据记录！");
            result.setDTS(null);
        } else {
            result.setRT_F("1");
            result.setRT_D("成功！");
            result.setDTS(list);
        }

        return JsonUtils.toJson(result);
    }

    /**
     * 获取知识库 工作程序和工作步骤列表 数据
     *
     * @param workNo 工作编号
     * @return json
     */
    public static String getNextProfessionList(String workNo) {
        JSBaseListResult<ControlProcess> result = new JSBaseListResult<>();

        if (TextUtils.isEmpty(workNo)) {
            result.setRT_F("20");
            result.setRT_D("请求参数错误！");
            result.setDTS(null);
        } else {
            List<ControlProcess> list = new KnowledgeCheck().getNextProfessionList(workNo);

            if (list == null || list.isEmpty()) {
                result.setRT_F("40");
                result.setRT_D("未查询到此数据记录！");
                result.setDTS(null);
            } else {
                result.setRT_F("1");
                result.setRT_D("成功！");
                result.setDTS(list);
            }
        }

        return JsonUtils.toJson(result);
    }

    /**
     * 获取知识库 业务分类列表 数据
     *
     * @param workNo 工作编号
     * @return json
     */
    public static String getProfessionList(String workNo) {
        JSBaseListResult<ControlProcess> result = new JSBaseListResult<>();

        if (TextUtils.isEmpty(workNo)) {
            result.setRT_F("20");
            result.setRT_D("请求参数错误！");
            result.setDTS(null);
        } else {
            List<ControlProcess> list = new KnowledgeCheck().getProfessionList(workNo);

            if (list == null || list.isEmpty()) {
                result.setRT_F("40");
                result.setRT_D("未查询到此数据记录！");
                result.setDTS(null);
            } else {
                result.setRT_F("1");
                result.setRT_D("成功！");
                result.setDTS(list);
            }
        }

        return JsonUtils.toJson(result);
    }

    /**
     * 获取知识库工作步骤详情
     *
     * @param workNo 工作编号
     * @return json
     */
    public static String getThirdDetail(String workNo) {
        JSBaseListResult<ControlProcessDetail> result = new JSBaseListResult<>();

        if (TextUtils.isEmpty(workNo)) {
            result.setRT_F("20");
            result.setRT_D("请求参数错误！");
            result.setDTS(null);
        } else {
            List<ControlProcessDetail> list = new KnowledgeCheck().getThirdDetail(workNo);

            if (list == null || list.isEmpty()) {
                result.setRT_F("40");
                result.setRT_D("未查询到此数据记录！");
                result.setDTS(null);
            } else {
                result.setRT_F("1");
                result.setRT_D("成功！");
                result.setDTS(list);
            }
        }

        return JsonUtils.toJson(result);
    }

    /**
     * 获取知识库 标准视频 列表
     *
     * @param workNo 工作编号
     * @return json
     */
    public static String getVideoList(String workNo) {
        JSBaseListResult<VideoStandard> result = new JSBaseListResult<>();

        if (TextUtils.isEmpty(workNo)) {
            result.setRT_F("20");
            result.setRT_D("请求参数错误！");
            result.setDTS(null);
        } else {
            List<VideoStandard> list = new KnowledgeCheck().getVideoList(workNo);

            if (list == null || list.isEmpty()) {
                result.setRT_F("40");
                result.setRT_D("未查询到此数据记录！");
                result.setDTS(null);
            } else {
                result.setRT_F("1");
                result.setRT_D("成功！");
                result.setDTS(list);
            }
        }

        return JsonUtils.toJson(result);
    }

    /**
     * 获取知识库 典型经验 列表
     *
     * @param workNo 工作编号
     * @return json
     */
    public static String getExperienceList(String workNo) {
        JSBaseListResult<TypicalExp> result = new JSBaseListResult<>();

        if (TextUtils.isEmpty(workNo)) {
            result.setRT_F("20");
            result.setRT_D("请求参数错误！");
            result.setDTS(null);
        } else {
            List<TypicalExp> list = new KnowledgeCheck().getExperienceList(workNo);

            if (list == null || list.isEmpty()) {
                result.setRT_F("40");
                result.setRT_D("未查询到此数据记录！");
                result.setDTS(null);
            } else {
                result.setRT_F("1");
                result.setRT_D("成功！");
                result.setDTS(list);
            }
        }

        return JsonUtils.toJson(result);
    }

    /**
     * 获取知识库 典型经验详情
     *
     * @param recordId 记录Id
     * @return json
     */
    public static String getExperienceDetail(String recordId) {
        JSBaseResult<TypicalExp> result = new JSBaseResult<>();

        if (TextUtils.isEmpty(recordId)) {
            result.setRT_F("20");
            result.setRT_D("请求参数错误！");
            result.setDTS(null);
        } else {
            TypicalExp data = new KnowledgeCheck().getExperienceDetail(recordId);

            if (data == null) {
                result.setRT_F("40");
                result.setRT_D("未查询到此数据记录！");
                result.setDTS(null);
            } else {
                result.setRT_F("1");
                result.setRT_D("成功！");
                result.setDTS(data);
            }
        }

        return JsonUtils.toJson(result);
    }

    /**
     * 查询操作步骤及风险点
     *
     * @param processNos 工作程序编号集合
     * @return json
     */
    public static String getProcessRiskList(List<String> processNos) {
        JSBaseListResult<ProcessRiskData> result = new JSBaseListResult<>();

        if (processNos == null || processNos.isEmpty()) {
            result.setRT_F("20");
            result.setRT_D("请求参数错误！");
            result.setDTS(null);
        } else {
            List<ProcessRiskData> list = new KnowledgeCheck().getProcessRiskList(processNos);

            if (list == null || list.isEmpty()) {
                result.setRT_F("40");
                result.setRT_D("未查询到此数据记录！");
                result.setDTS(null);
            } else {
                result.setRT_F("1");
                result.setRT_D("成功！");
                result.setDTS(list);
            }
        }

        return JsonUtils.toJson(result);
    }

    /**
     * 查询关键风险点取证列表
     *
     * @param stepNos 工作步骤编号集合
     * @return json
     */
    public static String getProcessRiskEvidenceList(List<String> stepNos) {
        JSBaseListResult<ProcessRiskData> result = new JSBaseListResult<>();

        if (stepNos == null || stepNos.isEmpty()) {
            result.setRT_F("20");
            result.setRT_D("请求参数错误！");
            result.setDTS(null);
        } else {
            List<ProcessRiskData> list = new KnowledgeCheck().getProcessRiskEvidenceList(stepNos);

            if (list == null || list.isEmpty()) {
                result.setRT_F("40");
                result.setRT_D("未查询到此数据记录！");
                result.setDTS(null);
            } else {
                result.setRT_F("1");
                result.setRT_D("成功！");
                result.setDTS(list);
            }
        }

        return JsonUtils.toJson(result);
    }

    /**
     * 查询关键风险点取证记录
     *
     * @param appNo   工单号
     * @param stepNos 工作步骤编号集合
     * @return json
     */
    public static String getRiskCheckStateList(String appNo, List<String> stepNos) {
        JSBaseListResult<RiskCheckState> result = new JSBaseListResult<>();

        if (TextUtils.isEmpty(appNo)
                || stepNos == null
                || stepNos.isEmpty()
        ) {
            result.setRT_F("20");
            result.setRT_D("请求参数错误！");
            result.setDTS(null);
        } else {
            List<RiskCheckState> list = new KnowledgeCheck().getRiskCheckStateList(appNo, stepNos);

            if (list == null || list.isEmpty()) {
                result.setRT_F("40");
                result.setRT_D("未查询到此数据记录！");
                result.setDTS(null);
            } else {
                result.setRT_F("1");
                result.setRT_D("成功！");
                result.setDTS(list);
            }
        }

        return JsonUtils.toJson(result);
    }

    /**
     * 查询标准化作业步骤及风险点
     *
     * @param appNo  工单编号
     * @param planNo 计划明细编号
     * @return json
     */
    public static String getTicketRiskList(
            String appNo,
            String appCode,
            String eomOrderSrc,
            String workTypeNo,
            String planNo,
            String isFilter,
            String isShowAppend,
            List<String> processNos
    ) {
        JSBaseResult<TicketMeasureResult> result = new JSBaseResult<>();

        if (TextUtils.isEmpty(appNo)
                && TextUtils.isEmpty(planNo)) {
            result.setRT_F("20");
            result.setRT_D("请求参数错误！");
            result.setDTS(null);
        } else {
            TicketMeasureResult data =
                    new KnowledgeCheck().getTicketRiskList(
                            appNo,
                            appCode,
                            eomOrderSrc,
                            workTypeNo,
                            planNo,
                            isFilter,
                            isShowAppend,
                            processNos);

            if (data == null
                    || (data.getKnowledge().isEmpty()
                    && data.getCustom().isEmpty())) {
                result.setRT_F("40");
                result.setRT_D("未查询到此数据记录！");
                result.setDTS(null);
            } else {
                result.setRT_F("1");
                result.setRT_D("成功！");
                result.setDTS(data);
            }
        }

        return JsonUtils.toJson(result);
    }

    /**
     * 查询标准化作业风险取证列表
     *
     * @param appNo  工单编号
     * @param planNo 计划明细编号
     * @return json
     */
    public static String getTicketRiskEvidenceList(
            String appNo,
            String appCode,
            String eomOrderSrc,
            String workTypeNo,
            String planNo,
            String isFilter,
            String isShowAppend,
            List<String> stepNos
    ) {
        JSBaseResult<TicketMeasureResult> result = new JSBaseResult<>();

        if (TextUtils.isEmpty(appNo)
                && TextUtils.isEmpty(planNo)) {
            result.setRT_F("20");
            result.setRT_D("请求参数错误！");
            result.setDTS(null);
        } else {
            TicketMeasureResult data =
                    new KnowledgeCheck().getTicketRiskEvidenceList(
                            appNo,
                            appCode,
                            eomOrderSrc,
                            workTypeNo,
                            planNo,
                            isFilter,
                            isShowAppend,
                            stepNos);

            if (data.getKnowledge().isEmpty()
                    && data.getCustom().isEmpty()) {
                result.setRT_F("40");
                result.setRT_D("未查询到此数据记录！");
                result.setDTS(null);
            } else {
                result.setRT_F("1");
                result.setRT_D("成功！");
                result.setDTS(data);
            }
        }

        return JsonUtils.toJson(result);
    }

    /**
     * 查询标准化作业关键风险点取证记录
     *
     * @param appNo  工单编号
     * @param planNo 计划明细编号
     * @return json
     */
    public static String getTicketRiskSaveList(
            String appNo,
            String appCode,
            String eomOrderSrc,
            String workTypeNo,
            String planNo
    ) {
        JSBaseResult<TicketMeasureSaveJS> result = new JSBaseResult<>();

        if (TextUtils.isEmpty(appNo)
                && TextUtils.isEmpty(planNo)) {
            result.setRT_F("20");
            result.setRT_D("请求参数错误！");
            result.setDTS(null);
        } else {
            List<TicketMeasureSave> list = new KnowledgeCheck()
                    .getTicketRiskSaveList(
                            appNo,
                            appCode,
                            eomOrderSrc,
                            workTypeNo,
                            planNo);

            if (list == null || list.isEmpty()) {
                result.setRT_F("40");
                result.setRT_D("未查询到此数据记录！");
                result.setDTS(null);
            } else {
                TicketMeasureSaveJS data = new TicketMeasureSaveJS();
                data.setAppNo(list.get(0).getAppNo());
                data.setPlanNo(list.get(0).getPlanNo());
                data.setWtNo(list.get(0).getWtNo());
                data.setMeasures(list);

                result.setRT_F("1");
                result.setRT_D("成功！");
                result.setDTS(data);
            }
        }

        return JsonUtils.toJson(result);
    }

    /* ===========================  离线请求方法 =============================== */

    /**
     * 获取工作票详情
     *
     * @param recordId 工作票编号
     * @return json
     */
    public static String getTicketDetail(String recordId) {
        JSBaseResult<TicketDetail> result = new JSBaseResult<>();

        if (TextUtils.isEmpty(recordId)) {
            result.setRT_F("20");
            result.setRT_D("请求参数错误！");
            result.setDTS(null);
        } else {
            TicketDetail data = new TicketLocalUtil().getTicketDetail(recordId);

            if (data == null) {
                result.setRT_F("40");
                result.setRT_D("未查询到此数据记录！");
                result.setDTS(null);
            } else {
                result.setRT_F("1");
                result.setRT_D("成功！");
                result.setDTS(data);
            }
        }

        return JsonUtils.toJson(result);
    }

    /**
     * 根据工作票类型查询设施列表
     *
     * @param wtType 工作票类型编号
     * @return json
     */
    public static String getTicketFaciList(String wtType) {
        JSBaseListResult<TicketForType> result = new JSBaseListResult<>();

        if (TextUtils.isEmpty(wtType)) {
            result.setRT_F("20");
            result.setRT_D("请求参数错误！");
            result.setDTS(null);
        } else {
            List<TicketForType> list =
                    new TicketForTypeImpl().findFaciByWtType(wtType);

            if (list == null || list.isEmpty()) {
                result.setRT_F("40");
                result.setRT_D("未查询到此数据记录！");
                result.setDTS(null);
            } else {
                result.setRT_F("1");
                result.setRT_D("成功！");
                result.setDTS(list);
            }
        }

        return JsonUtils.toJson(result);
    }

}
